package cn.easytogo.action.common;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.Resource;
import cn.easytogo.service.IResourceDAO;
import cn.easytogo.util.ConfigUtil;
import cn.easytogo.util.TreeUtil;

public class TreeAction extends BaseAction{
	private String admin;//后台标志位
	private IResourceDAO resourceDAO;
	/**
	 * 获取Tree数据(主要用于后台设置)
	 * */
	public void getTreeData(){
		Map map = new HashMap();
		map.put("admin", "1".equals(admin) ? "1":"0");
		List list = new ArrayList();
		try {
			list = resourceDAO.selectResources(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			list=null;
		}
		if(list==null||list.size()==0){//如果数据库中没有数据,则使用默认数据
			Map m = new HashMap();
			m.put("id", "1");
			m.put("name", "我的系统(此节点不可删除)");
			m.put("pid", "0");
			m.put("level", "1");
			list.add(m);
			if("1".equals(admin)&&logedUser!=null&&"admin".equals(logedUser.getUsername())){
				m = new HashMap();
				m.put("id", "2");
				m.put("name", "后台菜单维护");
				m.put("pid", "1");
				m.put("url", "admin/resource_toList.action");
				m.put("level", "2");
				list.add(m);
			}
		}
		String treejson = TreeUtil.getTreeJSON(list,"0", "id", "name", "pid", true, new String[]{"url","level"},true,null);   
		print(treejson);
	}
	/**
	 * 获取Tree数据(主要用于前台和后台的左侧树列表的展示)
	 * */
	public void getLeftTreeData(){
		List<Resource> resources = new ArrayList<Resource>();
		if(logedUser!=null){
			resources = logedUser.getResources();
		}
		String pids = "";
		List<String> idList = new ArrayList<String>();
		for (Resource r : resources) {//将所有资源遍历,找出所有的id
			idList.add(r.getId());
		}
		List resourceList = new ArrayList();
		for (Resource r : resources) {//遍历所有资源,如果在这些资源里有它的父资源,不做操作,否则,把它的pid设成0
			if(!idList.contains(r.getPid())){
				r.setPid("0");
				r.setLevel("2");
			}
			if(!"1".equals(ConfigUtil.getConfigByName("show_root_in_left_tree"))){//如果配置文件中没有特意标出要展示根节点,就不展示了
				if("1".equals(r.getPid())){
					r.setPid("0");
				}
				if("1".equals(r.getId())){
					continue;
				}
			}
			if(admin!=null&&r.getAdmin().trim().contains(admin)){
				Map m = new HashMap();
				m.put("id", r.getId());
				m.put("name", r.getName());
				m.put("pid", r.getPid());
				m.put("level", r.getLevel());
				m.put("url", r.getUrl());
				resourceList.add(m);
			}
		}
		String treejson = TreeUtil.getTreeJSON(resourceList,"0", "id", "name", "pid", true, new String[]{"url","level"},true,null);   
		print(treejson);
	}
	
	public String getAdmin() {
		return admin;
	}
	public void setAdmin(String admin) {
		this.admin = admin;
	}
	public IResourceDAO getResourceDAO() {
		return resourceDAO;
	}
	public void setResourceDAO(IResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}
}
